import { Component, OnInit } from '@angular/core';
import { CommonResponse,VerificationType,ToastColor,CommonService,AutoLoginType } from '@services/common-service.service';
import { AuthTokenService } from '@services/auth-token.service';
import { EventEmitterService,EventName } from '@services/event-emitter.service';
import { TranslateService } from '@ngx-translate/core';
import { DefaultLanguageService } from '@services/default-language.service';

@Component({
  selector: 'app-personal-info',
  templateUrl: './personal-info.page.html',
  styleUrls: ['./personal-info.page.scss'],
})
export class PersonalInfoPage implements OnInit {

  public defaultPhotoUrl:string = 'assets/images/ios/proof of identity@3x.png';

  public appUser:any = {
    userName:'',
    billingAddress:'',
    photoUrl:'assets/images/ios/proof of identity@3x.png'
  }

  constructor(
    private commonService:CommonService,
    private authTokenService:AuthTokenService,
    private eventEmitterService:EventEmitterService,
    private translate: TranslateService,
    private defaultLanguageService:DefaultLanguageService) {
      this.commonService.initTranslate([]); 
  }

  ngOnInit() {
    this.getAppUser();
    this.initEventMonitor();
  }

  initEventMonitor(){
    this.eventEmitterService.eventEmit.subscribe((value: any) => {
      if(value == EventName.USERNAME_PHOTO_CHANGED
        || value == EventName.BILLING_ADDRESS_CHANGED) {
        this.getAppUser();
      }
    });
  }

  getAppUser(){
    let appUserId = this.authTokenService.getAppUserId();
    this.commonService.getAppUser(appUserId).subscribe((response:CommonResponse)=>{
      if(response.isSuccess){
        this.appUser = response.responseBody;
        if(!this.appUser.photoUrl){
          this.appUser.photoUrl = this.defaultPhotoUrl;
        }
      }
    },(errorResponse)=>{
      this.commonService.commonToast(ToastColor.warning,errorResponse.error.errorMsg);
    });
  }

}
